﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.IO;

namespace PlanningGIS.CoordCovert.model
{
    public class CcDbServer
    {
        OleDbConnection oleConn = null;
        string MDB_LOCATION;
        string tablename = "PT";
        public string fldX, fldY, fldDx, fldDy;        //格网控制点的X、Y、X偏移量、Y偏移量的属性字段名
        /// <summary>
        /// 初始化
        /// </summary>
        public CcDbServer(string path,string tn)
        {
            fldX = "变形前X值";
            fldY = "变形前Y值";
            //fldX = "xbefore";
            //fldY = "ybefore";
            fldDx = "X偏移量";
            fldDy = "Y偏移量";

            MDB_LOCATION = path;
            tablename = tn;
            //MDB_LOCATION = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location) + "//TranDB_XY.mdb";
            //if (File.Exists(MDB_LOCATION) == false) return;
            oleConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + MDB_LOCATION);
            oleConn.Open();
        }

        

        public bool IsValidate()
        {
            if (oleConn == null || oleConn.State != ConnectionState.Open)
            {
                return false;
            }
            else
            {
                return true;
            }

        }

        public DataRow GetDr(double x, double y, double dis)
        {
            if (oleConn == null || oleConn.State != System.Data.ConnectionState.Open) return null;

            OleDbCommand dbCmd = oleConn.CreateCommand();
            dbCmd.CommandText = "select * from " + tablename + " where " + fldX + " = '" + x.ToString() + "' and " + fldY + " = '" + y.ToString() + "'";
            //dbCmd.CommandText = "select * from " + tablename + " where 变形前X值  = " + x + " and  变形前Y值 = " + y;

            //dbCmd.CommandText = "select * from " + tablename + " where " + fldX + " < " + (x + dis / 2) + " and " + fldX + " > " + (x - dis / 2)
            //    + " and " + fldY + " < " + (y + dis / 2) + " and " + fldY + " > " + (y - dis / 2); 

            IDbDataAdapter pAdp = new OleDbDataAdapter(dbCmd);
            DataSet dataset = new DataSet();
            pAdp.Fill(dataset);
            if (dataset.Tables[0].Rows.Count == 0) return null;

            return dataset.Tables[0].Rows[0];
        }

        public DataTable GetDt()
        {
            if (oleConn == null || oleConn.State != System.Data.ConnectionState.Open) return null;

            OleDbCommand dbCmd = oleConn.CreateCommand();
            dbCmd.CommandText = "select * from " + tablename + " order by " + fldY + " desc," + fldX;
            IDbDataAdapter pAdp = new OleDbDataAdapter(dbCmd);
            DataSet dataset = new DataSet();
            pAdp.Fill(dataset);
            if (dataset.Tables[0].Rows.Count == 0) return null;
            return dataset.Tables[0];
        }

        public int GetCount(string sql)
        {
            if (oleConn == null || oleConn.State != System.Data.ConnectionState.Open) return 0;
            OleDbCommand dbCmd = oleConn.CreateCommand();
            dbCmd.CommandText = "select * from " + tablename + " where " + sql;

            IDbDataAdapter pAdp = new OleDbDataAdapter(dbCmd);
            DataSet dataset = new DataSet();
            pAdp.Fill(dataset);

            return dataset.Tables[0].Rows.Count;
        }
        /// <summary>
        /// 析构函数
        /// </summary>
        public void Finalize()
        {
            if (oleConn != null && oleConn.State == System.Data.ConnectionState.Open) oleConn.Close();
        }
    }
}
